

var treeItem = function (data, options, parent, level) {
    this.html = $('<div class="form_select_tree_item" style="padding-left:' + level * 10 + 'px"></div>');
    this.name = $('<div class="form_select_tree_item_name"></div>').appendTo(this.html);
    var _this = this;
    this.icon = $('<i class="fa fa-plus-square-o" aria-hidden="true"></i>').appendTo(this.name);
    this.text = $('<span><i class="fa fa-folder" aria-hidden="true"></i>' + data[options.showKey] + '</span>').appendTo(this.name);
    this.box = $('<div class="form_select_tree_item_box"></div>').appendTo(this.html);
    this.data = data;
    parent.BroupByObj[data[parent.options.groupByKey]] = this;
    this.box.hide();
    this.html.click(function (e) {
        parent.active(_this);
        e.stopPropagation();
    });
    this.icon.css({
        'line-height': '35px',
        'vertical-align': 'middle',
        'height': '35px',
        'display': 'inline-block',
        'width': '20px',
        'text-align': 'center',
    });
    this.icon.click(function (e) {
        if (_this.icon.hasClass('fa-plus-square-o')) {
            _this.icon.removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
            _this.box.show();
        } else {
            _this.icon.removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
            _this.box.hide();
        };
        e.stopPropagation();
    });


    if (level == 0) {
        _this.icon.removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
        _this.box.show();
    };


    this.children = [];
    if (data.hasOwnProperty(parent.options.childrenKey) && data[parent.options.childrenKey].length) {
        for (let i = 0; i < data[parent.options.childrenKey].length; i++) {
            const element = data[parent.options.childrenKey][i];
            var item = new treeItem(element, options, parent, level + 1);
            this.children.push(item);
            this.box.append(item.html);
        }
    } else {
        this.icon.hide();
        this.text.find('i').removeClass('fa-file').addClass('fa-folder');
        this.html.css({
            'padding-left': (level * 10 + 25) + 'px',
        });
    };
};
treeItem.prototype.get = function () {
    return this.data;
};
treeItem.prototype.active = function () {
    this.name.addClass('active');
};
treeItem.prototype.unActive = function () {
    this.name.removeClass('active');
};
var chooseBase = function (options) {
    this.options = $.extend({}, {
        placeholder: '',
        childrenKey: 'children',
        showKey: 'content',
        groupByKey: 'code',
        getKey: 'code',
        dataType: 'tree',
        className: '',
        errorStyle: 'form-error',
    }, options);
    var html = '';
    html += '   <div class="form_select form_select_tree ' + this.options.className + '">';
    html += '       <div class="form_select_nameBox">';
    html += '           <div class="form_select_name">' + this.options.placeholder + '</div>';
    html += '       </div>';
    html += '       <div class="form_select_optionBox"></div>';
    html += '   </div>';
    this.html = $(html);
    this.nameBox = this.html.find('.form_select_nameBox');
    this.name = this.html.find('.form_select_name');
    this.optionBox = this.html.find('.form_select_optionBox');
    this.showOptionBox = false;
    this.document = null;
    this.bindClick();
    this.optionBoxChild = [];
    this.BroupByObj = {};
    this.renderData = [];
    this.activeObj = null;
};
chooseBase.prototype.setData = function (data) {
    this.BroupByObj = {};
    this.renderData = data;
    var _this = this;
    var getObj = function (data) {
        for (var i = 0; i < data.length; i++) {
            var element = data[i];
            _this.BroupByObj[element[_this.options.groupByKey]] = element;
            if (element.hasOwnProperty(_this.options.childrenKey) && element[_this.options.childrenKey].length) {
                getObj(element[_this.options.childrenKey]);
            };
        };
    };
    getObj(data);
    for (var i = 0; i < data.length; i++) {
        var element = data[i];
        var item = new treeItem(element, this.options, this, 0);
        this.optionBoxChild.push(item);
        this.optionBox.append(item.html);
    };
};
chooseBase.prototype.set = function (data) {
    if (this.BroupByObj.hasOwnProperty(data)) {
        var item = this.BroupByObj[data];
        this.active(item);
    };
};

chooseBase.prototype.active = function (item) {
    if (this.activeObj) {
        this.activeObj.unActive();
    };
    this.activeObj = item;
    item.active();
    this.name.text(item.data[this.options.showKey]);
    this.documentClick();
    this.hideErrorStyle();
};
// 清除选择方法
chooseBase.prototype.clear = function () {
    this.valueData = null;
    this.value = null;
    this.name.text(this.options.placeholder);
    return this;
};
// 绑定点击事件
chooseBase.prototype.bindClick = function () {
    var _this = this;
    _this.documentClick = function () {
        _this.showOptionBox = false;
        _this.optionBox.css('display', 'none');
        $(document).unbind('click', _this.documentClick);
    };
    this.nameBox.unbind('click').bind('click', function (e) {
        if (_this.showOptionBox) {
            _this.documentClick();
        } else {
            _this.showOptionBox = true;
            _this.optionBox.css('display', 'block');
            $(document).click();
            $(document).unbind('click', _this.documentClick).bind('click', _this.documentClick);
        };
        e.stopPropagation();
    });
    return this;
};
chooseBase.prototype.showErrorStyle = function () {
    if (!this.options.errorStyle) {
        return this;
    };
    this.html.addClass(this.options.errorStyle);
    return this;
};
chooseBase.prototype.hideErrorStyle = function () {
    if (!this.options.errorStyle) {
        return this;
    };
    this.html.removeClass(this.options.errorStyle);
    return this;
};

chooseBase.prototype.get = function (callBack, ignore) {
    if (this.activeObj) {
        callBack(null, this.activeObj.data);
    } else {
        callBack('请至少选择一项');
        this.showErrorStyle();
    };
    return this;
};;
export default chooseBase;
